<?php 
	namespace app\admin\controller;
	use think\Controller;
	use think\Db;
	//后台主页
	class power extends Allow
	{
		public function getEdit()
		{
			$request=request();
			//拿到权限等级
			$details=$request->param('details');
			$datas=Db::table('users')->where('details',$details)->find();
			//查询权限对应的列表
		    $data=Db::table('lists')->where('details',$details)->select();
			   //便利
			  $arr=[];
		  if($data)
		  {
            foreach($data as $k=>$v)
             {
             //放入数组,方便查询权限
             $arr[]=$v['nid'];
             }
          }
          //获取全部权限
          $list=Db::query("select *,concat(path,',',id) as paths from list order by paths;");
		 return $this->fetch('power/edit',['list'=>$list,'arr'=>$arr,'details'=>$details,'datas'=>$datas]);
			
		}
		public function postUpdate()
		{
			//获取权限
			$request=request();
		$details=$request->param('details');
		$data=$request->only(['details','node']);
           //删除原来的权限\
          if($data['details']!=0){
		   $list=Db::table('lists')->where('details',$data['details'])->delete();
	     
			if($list)
           {
          //删除成功添加新权限
          foreach($data['node'] as $k=>$v)
          {
           $result=Db::table('lists')->insert(['details'=>$data['details'],'nid'=>$v]);
          }
          if($result){
            //修改成功
             $this->success('权限修改成功','/users/index');
          }else{
            //修改失败
             $this->error('权限修改失败..','/users/index');
          }

          
         }else{
          $this->error('权限修改失败..','/users/index');
         }
        }else
        {
        	//权限为0执行添加
        	if(empty($data['node'])){
        		$result=Db::table('lists')->where('details','0')->delete();
        		if($result){
            //修改成功
             $this->success('权限修改成功','/users/index');
              }else{
            //修改失败
             $this->error('权限修改失败..','/users/index');
             }

        	}
           foreach($data['node'] as $k=>$v)
          {
           $result=Db::table('lists')->insert(['details'=>$data['details'],'nid'=>$v]);
          }
          if($result){
            //修改成功
             $this->success('权限修改成功','/users/index');
          }else{
            //修改失败
             $this->error('权限修改失败..','/users/index');
          }
        }

		}
		
	}
 ?>